package cn.wen.yaling.controller;

import java.util.List;
import java.util.Map;

import cn.wen.yaling.commons.enums.LimitNum;
import cn.wen.yaling.commons.enums.TypeSort;
import cn.wen.yaling.commons.vo.*;
import cn.wen.yaling.xo.service.ArticleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.TypeEntity;
import cn.wen.yaling.xo.service.TypeService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;



/**
 * 
 *
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private ArticleService articleService;


    /**
     * 获取当前激活的全部分类 文章分类
     * @return
     */
    @GetMapping("/web/type/list")
    public ResultEntity getTypeAllList() {
        List<TypeVO> typeVOList = typeService.getTypeAllList();
        return ResultEntity.ok().put("data", typeVOList);
    }

    /**
     * 获取当前分类下的所有文章列表
     * @return
     */
    @GetMapping("/web/type/article-list")
    public ResultEntity getTypeArticleList(@RequestParam("typeId") Integer typeId) {
        List<ArticleListVO> articleListVOS = typeService.getTypeArticleList(typeId);
        return ResultEntity.ok().put("data", articleListVOS);
    }

    /**
     * 当前分类的最热文章
     * @param typeId
     * @return
     */
    @GetMapping("/web/type/article-hot")
    public ResultEntity getTypeHotArticleList(@RequestParam("typeId") Integer typeId) {
        List<ArticleSimpleVO> articleListVOS = articleService.getArticleHotListByTag(typeId,  LimitNum.EIGHT.getNum());
        return ResultEntity.ok().put("data", articleListVOS);
    }


    /** ============================= 后台 ==================================== **/
    /**
     * 获取当前激活的全部分类 文章分类
     * @return
     */
    @GetMapping("/admin/type/list")
    public ResultEntity getAllTypeList() {
        List<TypeEntity> types = typeService.getTypeList();
        return ResultEntity.ok().put("data", types);
    }

    /**
     * 查询分类列表
     * @return
     */
    @GetMapping("/admin/type/search/list")
    public ResultEntity searchType(@RequestParam("typeName") String typeName) {
        List<TypeEntity> types = typeService.searchType(typeName);
        return ResultEntity.ok().put("data", types);
    }

    /**
     * 添加分类
     * @return
     */
    @PostMapping("/admin/type/add")
    public ResultEntity addType(@RequestBody TypeAddVO type) {
        return typeService.addType(type);
    }

    /**
     * 删除分类
     * @return
     */
    @PostMapping("/admin/type/delete")
    public ResultEntity deleteType(@RequestParam("typeIds") Integer[] typeIds) {
        return typeService.deleteTypeByIds(typeIds);
    }

    /**
     * 删除分类
     * @return
     */
    @PostMapping("/admin/type/update")
    public ResultEntity updateType(@RequestBody TypeUpdateVO type) {
        return typeService.updateTypeById(type);
    }

    /**
     * 修改类别状态
     * @return
     */
    @PostMapping("/admin/type/update-status")
    public ResultEntity updateTypeStatus(@RequestParam("typeId") Integer typeId,
                                         @RequestParam("status") Integer status) {
        return typeService.updateTypeStatusById(typeId, status);
    }




}
